package unito.likir.util;

import java.util.Collection;

import unito.likir.security.MTRandom;

/**
 * List utilities 
 * @author Luca Maria Aiello
 */
public class ListUtils
{

	/**
	 * Durstenfeld's algorithm of Fisher-Yates shuffle
	 * @param list the list to be shuffled
	 */
	@SuppressWarnings("unchecked")
	public static <E> void shuffle(Collection<E> list)
	{
		MTRandom rand = new MTRandom();
		
		Object[] arr = list.toArray();
		
		int n = arr.length;     
		while (n > 1) 
        {
            int k = rand.getUniform(0, n-1);  // 0 <= k < n.
            n--;                     // n is now the last pertinent index;
            Object temp = arr[n];     // swap array[n] with array[k] (does nothing if k == n).
            arr[n] = arr[k];
            arr[k] = temp;
        }
		list.clear();
		for (Object obj : arr)
			list.add((E)obj);
	}
	
	/*public static void main(String... args)
	{
		int[] arr = {1,2,3,4,5,6,7,8,9};
		LinkedList<Integer> list = new LinkedList<Integer>();
		for (int i : arr)
			list.add(i);
		ListUtils.shuffle(list);
		for (int i : list)
			System.out.println(" - " + i);
	}*/
}
